<?php

declare(strict_types=1);

namespace App\CustHub\Model;

use Mine\MineModel;

/**
 * @property int $id 
 * @property int $uid 
 * @property int $main_uid 
 * @property string $name 客服名称
 * @property string $account 客服账号
 * @property int $online_status 在线状态：0=下线 1=在线
 * @property array $assign_rules 分配条件
 * @property array $work_time 工作时间
 * @property string $auto_reply 自动回复内容
 * @property int $max_sessions 接待上限
 * @property int $auto_accept_reply 回复后是否自动转为已接待：0-否，1-是
 * @property int $accept_after_reply_minutes 回复后多少分钟标记为已接待,0代表立即
 * @property int $delete_on_accept 是否在已接待后立即删除会话：0-否，1-是
 * @property int $delete_after_accept_minutes 会话“已接待”后多少分钟自动删除 0代表立即
 * @property int $status 状态：0=禁用 1=正常
 * @property \Carbon\Carbon $created_at 
 * @property \Carbon\Carbon $updated_at 
 * @property-write mixed $password 密码
 */
class ChCustomerService extends MineModel
{
    
    /**
     * 状态
     */
    public const ENABLE = 1;
    public const DISABLE = 0;

    /**
     * The table associated with the model.
     */
    protected ?string $table = 'ch_customer_service';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['id', 'uid', 'main_uid', 'name', 'account', 'password', 'online_status', 'assign_rules', 'work_time', 'auto_reply', 'max_sessions', 'auto_accept_reply', 'accept_after_reply_minutes', 'delete_on_accept', 'delete_after_accept_minutes', 'status', 'created_at', 'updated_at'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'uid' => 'integer', 'main_uid' => 'integer', 'online_status' => 'integer', 'assign_rules' => 'array', 'work_time' => 'array', 'max_sessions' => 'integer', 'auto_accept_reply' => 'integer', 'accept_after_reply_minutes' => 'integer', 'delete_on_accept' => 'integer', 'delete_after_accept_minutes' => 'integer', 'status' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];

    public function robots(): \Hyperf\Database\Model\Relations\BelongsToMany
    {
        return $this->belongsToMany(ChRobot::class, 'ch_customer_service_robot',  'customer_service_id','robot_id')->withPivot('sort');
    }

    public function rooms()
    {
        return $this->belongsToMany(ChRoom::class, 'ch_customer_service_room', 'customer_service_id', 'room_id');
    }

    /**
     * 密码加密.
     * @param mixed $value
     */
    public function setPasswordAttribute($value): void
    {
        $this->attributes['password'] = password_hash($value, PASSWORD_DEFAULT);
    }

    /**
     * 验证密码
     * @param mixed $password
     * @param mixed $hash
     */
    public static function passwordVerify($password, $hash): bool
    {
        return password_verify($password, $hash);
    }
}
